//
//  BSTBitmapView.m
//  LXBST
//
//  Created by bst on 2018/9/20.
//  Copyright © 2018年 WPP. All rights reserved.
//
/**
 1.一个图片
 2.一个lab就OK啦
 3.后期更改骨骼动画/lottie动画
 */
#import "BSTBitmapView.h"
#import <FLAnimatedImage/FLAnimatedImageView.h>
#import <FLAnimatedImage/FLAnimatedImage.h>
@interface BSTBitmapView ()
@property(nonatomic,strong)FLAnimatedImage *animatedImage;
@property(nonatomic,weak)FLAnimatedImageView *bitmapIV;
@property(nonatomic,weak)UILabel *contentLab;
@end

@implementation BSTBitmapView
- (instancetype)init
{
    if (self = [super init]) {
        self.clipsToBounds = YES;
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.clipsToBounds = YES;
    }
    return self;
}
- (void)changeBaike
{
    self.contentLab.text = @"没有找到您要的答案，试试去提问";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:button];
    button.frame = CGRectMake(0, 0, 180*ADAPTER_WIDTH, 36*ADAPTER_WIDTH);
    button.titleLabel.font = kFontRegular14;
    [button setTitle:@"去提问" forState:UIControlStateNormal];
    [button setTitleColor:WhiteColor forState:UIControlStateNormal];
    button.layer.cornerRadius = 18*ADAPTER_WIDTH;
    button.layer.masksToBounds = YES;
    button.backgroundColor = [UIColor ColorGradientType:ColorGradientTypeHorizontal andWithSize:button.size andWithColors:@[(__bridge id)rgba(255, 146, 92, 1).CGColor,(__bridge id)rgba(255, 66, 66, 1).CGColor]];
    button.top = self.contentLab.bottom + 20*ADAPTER_WIDTH;
    button.centerX = self.contentLab.centerX;
    [button addTarget:self action:@selector(clickTouchEvent:) forControlEvents:UIControlEventTouchUpInside];
    
}
#pragma mark - 切换类型
- (void)setType:(BitmapViewType)type
{
    _type = type;
    [self removeSelfSubViews];
    self.backgroundColor = kBGDarkColor;
    switch (type)
    {
        case BitmapViewTypeLoading:
        {
            self.bitmapIV.animatedImage = self.animatedImage;
            self.bitmapIV.frame = CGRectMake((self.width - 90*ADAPTER_WIDTH) * 0.5, (self.height - 65*ADAPTER_WIDTH) * 0.5, 90*ADAPTER_WIDTH, 65*ADAPTER_WIDTH);
        }
            break;
        case BitmapViewTypeNoData:
        {
            self.bitmapIV.image = [UIImage getPNGimageInBundleWithName:@"BSTBitmap_nodata"];
            self.bitmapIV.frame = CGRectMake((self.width - 194*ADAPTER_WIDTH) * 0.5, 130*ADAPTER_WIDTH, 194*ADAPTER_WIDTH, 167*ADAPTER_WIDTH);
            self.contentLab.text = @"空空如也~";
            self.contentLab.textColor = kColor153;
            self.contentLab.font = kFontRegular14;
            self.contentLab.textAlignment = NSTextAlignmentCenter;
            self.contentLab.frame = CGRectMake(10*ADAPTER_WIDTH, self.bitmapIV.bottom + 10*ADAPTER_WIDTH, self.width - 20*ADAPTER_WIDTH, 20*ADAPTER_WIDTH);
        }
            break;
        case BitmapViewTypeNetError:
        {
            self.bitmapIV.image = [UIImage getPNGimageInBundleWithName:@"BSTBitmap_neterror"];
            self.bitmapIV.frame = CGRectMake(0, -150*ADAPTER_WIDTH, WIDTH, HEIGHT);
        }
            break;
        case BitmapViewTypeServerCrash:
        {
            self.bitmapIV.image = [UIImage getPNGimageInBundleWithName:@"BSTBitmap_404"];
            self.bitmapIV.frame = CGRectMake(0, -150*ADAPTER_WIDTH, WIDTH, HEIGHT);
        }
            break;
        case BitmapViewTypeNormal:
        {
            self.backgroundColor = rgba(0, 0, 0, 0);
        }
            break;
        case BitmapViewTypeNOLogin:
        {
            self.backgroundColor = rgba(0, 0, 0, 0);
        }
            break;
        default:
            break;
    }
}
#pragma mark - 删除之前的所有子视图
- (void)removeSelfSubViews
{
     [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         [obj removeFromSuperview];
     }];
    _bitmapIV = nil;
    _contentLab = nil;
}
#pragma mark - 点击事件
- (void)clickTouchEvent:(UITapGestureRecognizer *)tap
{
    if ([self.delegate respondsToSelector:@selector(bstBitmapView:backType:)]) {
        [self.delegate bstBitmapView:self backType:self.type];
    }
}
#pragma mark - Lazy懒加载区域
- (FLAnimatedImageView *)bitmapIV
{
    if (!_bitmapIV)
    {
        FLAnimatedImageView *iv = [[FLAnimatedImageView alloc]init];
        //iv.runLoopMode = NSDefaultRunLoopMode;
        iv.runLoopMode = NSRunLoopCommonModes;
        [self addSubview:iv];
        iv.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTouchEvent:)];
        iv.userInteractionEnabled = YES;
        [iv addGestureRecognizer:tap];
        iv.contentMode = UIViewContentModeScaleAspectFit;
        _bitmapIV = iv;
    }
    return _bitmapIV;
}
- (UILabel *)contentLab
{
    if (!_contentLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.numberOfLines = 0;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTouchEvent:)];
        lab.userInteractionEnabled = YES;
        [lab addGestureRecognizer:tap];
        _contentLab = lab;
    }
    return _contentLab;
}
- (FLAnimatedImage *)animatedImage
{
    if (!_animatedImage) {
        _animatedImage = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"BSTBitmap_loading" ofType:@"gif"]]];
    }
    return _animatedImage;
}
- (void)dealloc
{
    NSLog(@"BSTBitmapView ---dealloc");
}
@end


/**
 这是重点 后期研究研究 动画不动 所以加上这行 比如程序立即启动时候的会出现
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     [self.bitmapIV stopAnimating];
 });
 
 //普通视图
 UIImage *image = [UIImage imageNamed:@"prompt_blank_page_icon"];
 self.bitmapIV.image = image;
 CGSize size = image.size;
 self.bitmapIV.frame = CGRectMake((self.width - size.width * 0.5) * 0.5, (self.height - size.height * 0.5) * 0.5, size.width * 0.5, size.height * 0.5);
 
 self.contenLab.textAlignment = NSTextAlignmentCenter;
 self.contenLab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
 self.contenLab.textColor = kColor153;
 self.contenLab.text = @"这里空空的,什么都没有";
 self.contenLab.frame = CGRectMake(0, self.bitmapIV.bottom + 4.5*ADAPTER_WIDTH, self.width, 30*ADAPTER_WIDTH);
 
 self.backgroundColor = kBGDarkColor;
 
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     [self.bitmapIV stopAnimating];
 });
 
 //loading动画
 self.backgroundColor = kBGDarkColor;
 
 NSMutableArray *refreshingImages = [NSMutableArray array];
 for (NSInteger index = 1; index <= 10 ; index ++) {
     UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_mj_refresh_refreshing_%ld",index]];
     [refreshingImages addObject:image];
 }
 self.bitmapIV.frame = CGRectMake((self.width - 90*ADAPTER_WIDTH) * 0.5, (self.height - 51*ADAPTER_WIDTH) * 0.5, 90*ADAPTER_WIDTH, 51*ADAPTER_WIDTH);
 self.bitmapIV.animationImages = refreshingImages;
 [self.bitmapIV startAnimating];
 
 self.contenLab.frame = CGRectZero;
 
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     [self.bitmapIV startAnimating];
 });
 */
